<?php
/*
*
* @package phpBB3
* @version $Id: rss.php,v 1.0 2007/11/15 14:19:15 mikov Exp $
* @copyright (c) phpbbchina.com
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/rss_config.'.$phpEx);
include($phpbb_root_path . 'includes/rss_functions.'.$phpEx);

if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
{
    $deadline=strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
	if($CFG['cache_time']>0) if((time()-$deadline)<$CFG['cache_time'])
	{
        exit_with_header("304 Not Modified");
	}
}

// Start session management
$user->session_begin();

$auth->acl($user->data);

$user->setup();

// requests begin
$fid    = request_var('f', '');
$count 	= request_var('c', $CFG['default_items']);
$rss_type 	= request_var('r', '');
$topics_only = request_var('t', 0);
$no_limit= request_var('nl', false);

$count = ( $count < 1 || $count > $CFG['max_items']) ? $CFG['max_items'] : $count;


// generate url
$board_url = generate_board_url();
$index_url = $board_url . '/index.' . $phpEx;
$viewtopic_url = $board_url . '/viewtopic.' . $phpEx;

//
// BEGIN SQL statement to fetch active posts of allowed forums
//
$sql_limit_by_http = '';
$max_record_age = time() - $CFG['max_days_ago']*86400;
$sql_limit_time = ($CFG['max_days_ago']>0) ? "p.post_time >".$max_record_age:"1";

if (!$no_limit){
	if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
		$NotErrorFlag = true;
		$not_modified_since = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
		if ($CFG['see_modified']) 
		{
			$sql_limit_by_http =  "AND (p.post_time > " . $not_modified_since . " OR p.post_edit_time >" . $not_modified_since . " )";
		} 
		else if ($not_modified_since > $max_record_age) 
		{
			$sql_limit_time = "p.post_time > ".$not_modified_since;
		}
	}
}

$sql_where = '';

// only topic first post
if ($topics_only == 1) 
{
	$sql_where = 'AND p.post_id = t.topic_first_post_id';
	$sql_from = 'FROM ' . POSTS_TABLE . ' as p, ' . FORUMS_TABLE . ' as f, ' . USERS_TABLE . ' as u, ' . TOPICS_TABLE . ' as t';
}
else
{
	$sql_from = 'FROM ' . POSTS_TABLE . ' as p, ' . FORUMS_TABLE . ' as f, ' . USERS_TABLE . ' as u';
}

// Exclude forums
if ($CFG['exclude_fids'])
{
	$exclude_fids = explode(',', $CFG['exclude_fids']);
	foreach ($exclude_fids as $i => $id)
	{
		if ($id > 0)
		{
			$sql_where .= ' AND p.forum_id != ' . trim($id);
		}
	}
}

if ($fid != '')
{
	$select_forums = explode(',', $fid);
	$sql_where .= ( sizeof($select_forums)>0 ) ? ' AND f.forum_id IN (' . $fid . ')' : '';
}

// SQL posts table
$sql = 'SELECT p.poster_id, p.post_subject, p.post_text, p.bbcode_uid, p.bbcode_bitfield, p.post_id, p.forum_id, p.post_time, f.forum_name, f.forum_desc_options, u.username
		' . $sql_from . '
		WHERE 
		' . $sql_limit_time . '  
		' . $sql_limit_by_http . '
		AND u.user_id = p.poster_id
		AND p.post_approved = 1
		AND (f.forum_id = p.forum_id)
		' . $sql_where . ' 
		ORDER BY post_time DESC LIMIT '.$count;

$result = $db->sql_query($sql, $CFG['expire']);

while( ($row = $db->sql_fetchrow($result)) )
{
	if (!$auth->acl_get('f_list', $row['forum_id']))
	{
		continue;
	}

	$template->assign_block_vars('item', array(
		'AUTHOR'		=> $row['username'],
		'TIME'			=> format_date($row['post_time']),
		'DISPLAYTIME'	=> $user->format_date($row['post_time']),
		'LINK'			=> append_sid("$board_url/viewtopic.$phpEx", 'f=' . $row['forum_id'] . '&amp;p=' . $row['post_id'] . '#p' . $row['post_id']),
		'IDENTIFIER'	=> append_sid("$board_url/viewtopic.$phpEx", 'f=' . $row['forum_id'] . '&amp;p=' . $row['post_id'] . '#p' . $row['post_id']),
		'TITLE'			=> '[' . $row['forum_name'] . ']'.$row['post_subject'],
		'TEXT'			=> utf8_clean_string(parse_message($row['post_text'], $row['forum_desc_options'], $row['bbcode_uid'], $row['bbcode_bitfield'])),		
		)
	);	
}

if ($rss_type == 'atom')
{
	$template->assign_var('FEED_LINK', $board_url);
	$content_type = 'application/atom+xml';
	$tpl = 'atom';
}
else
{
	$content_type = 'application/rss+xml';
	$tpl = 'rss2';
}

$template->set_filenames(array(
	'body' => 'rss_' . $tpl . '.xml')
);

// get time, use current time
$last_build_date = mktime();

if (defined('DEBUG'))
{
	$mtime = explode(' ', microtime());
	$totaltime = $mtime[0] + $mtime[1] - $starttime;
}

$template->assign_vars(array(
	'HEADER'		=> '<?xml version="1.0" encoding="UTF-8"?>' . "\n", // workaround for remove_php_tags() removing this line from the template
	'TITLE'			=> strip_tags($config['sitename']),
	'DESCRIPTION'	=> strip_tags($config['site_desc']),
	'LINK'			=> $board_url,
 	'LAST_BUILD'	=> format_date($last_build_date),
 	'FOOTER'		=> sprintf('<!-- Page generation time: %.3fs -->', $totaltime)
	)
);

// gzip compression
if ($config['gzip_compress'])
{
	if (@extension_loaded('zlib') && !headers_sent())
	{
		ob_start('ob_gzhandler');
	}
}

// start output
header ('Content-Type: ' . $content_type . '; charset=UTF-8');
$template->display('body');
exit;

?>